<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('结算单明细列表')"/>
</head>
<body class="gray-bg">

<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">客户姓名：</label>
                            <label class="col-sm-6 control-label">[[${statement.customerName}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">联系方式：</label>
                            <label class="col-sm-6 control-label">[[${statement.customerPhone}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">

                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">车牌号码：</label>
                            <label class="col-sm-6 control-label">[[${statement.licensePlate}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">汽车类型：</label>
                            <label class="col-sm-6 control-label">[[${statement.carSeries}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">服务类型：</label>
                            <label class="col-sm-6 control-label">[[${@dict.getLabel('si_service_catalog',statement.serviceType)}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">到店时间：</label>
                            <label class="col-sm-6 control-label"
                                   th:text="${#dates.format(statement.actualArrivalTime, 'yyyy-MM-dd HH:mm')}"></label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">总消费金额：</label>
                            <label class="col-sm-6 control-label" id="totalAmount">[[${statement.totalAmount}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">实付价格：</label>
                            <label class="col-sm-6 control-label" id="actuallyPaid">[[${statement.totalAmount-statement.discountAmount}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">优惠价格：</label>
                            <input class="col-sm-6" type="number" id="discountAmount" onchange="updateAmount()"
                                   name="discountAmount"
                                   th:field="${statement.discountAmount}"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="btn-group-sm" id="item-toolbar" role="group">
                <a id="saveBtn" class="btn btn-success" onclick="saveFun()"
                   shiro:hasPermission="business0:0s00tatementItem:add">
                    <i class="fa fa-plus"></i> 保存
                </a>
                <a id="payBtn" class="btn btn-info" onclick="payFun()"
                   shiro:hasPermission="business:statementItem:payStatement">
                    <i class="fa fa-cc-visa"></i> 确认支付
                </a>

            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="item-table"></table>
            </div>
        </div>
    </div>
</div>
<div class="ui-layout-east">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="serviceItemFormId">
                <div class="select-list">
                    <ul>
                        <li>
                            名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            是否套餐：
                            <select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            服务分类：
                            <select name="serviceCatalog" th:with="type=${@dict.getType('si_service_catalog')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
    </div>
    <div class="col-sm-12 select-table table-striped">
        <table id="serviceItem-table"></table>
    </div>
</div>
</div>

<th:block th:include="common/include :: footer"/>
<th:block th:include="common/include :: layout-latest"/>
</body>
<script th:inline="javascript">
    $('body').layout({
        east__size: '35%',
    });

    //支付
    function payFun() {
        $.modal.confirm("确认支付?", function () {
            var statementId = [[${statement.id}]];
            $.operate.post(prefix2 + "/payFun?statementId=" + statementId);
            $.table.refresh("item-table");
        })
    }

    //保存
    function saveFun() {
        $.modal.confirm("确认保存吗", function () {

            var discountAmount = $('#discountAmount').val();//消费金额
            var statementId = [[${statement.id}]];
            var boot = $('#item-table').bootstrapTable("getData", false);


            // var list = $.each(boot, function (index, item) {
            //     var lists = {
            //
            //         itemId: item.itemId,
            //         itemName: item.itemName,
            //         itemPrice: item.itemPrice,
            //         itemQuantity: item.itemQuantity
            //     };
            //     return lists;
            // });

            var config = {
                url: ctx + "/business/statementItem/saveItems",
                type: 'post',
                dataType: 'json',
                contentType: "application/json",
                data: JSON.stringify({"list": boot, "discountAmount": discountAmount, "statementId": statementId}),
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                },
                success: function (result) {
                    // $("#saveBtn").addClass("disabled");
                    // $("#payBtn").removeClass("disabled");
                    $.operate.ajaxSuccess(result);
                    $.table.refresh("item-table");
                }
            };
            $.ajax(config);

        });

    }

    var prefix2 = ctx + "/business/statementItem";
    $(function () {
        var options = {
            id: "item-table",
            toolbar: 'item-toolbar',
            uniqueId: "itemId",
            url: prefix2 + "/query?statementId=" + [[${statement.id}]],
            modalName: "服务",
            columns: [
                {
                    field: 'itemId',
                    title: '服务项id',
                    visible: false
                },
                {
                    field: 'itemName',
                    title: '服务项明细名称',
                    sortable: true
                },
                {
                    field: 'itemPrice',
                    title: '服务项价格',
                    sortable: true
                },
                {
                    field: 'itemQuantity',
                    title: '购买数量',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a  class="btn btn-info btn-xs " href="javascript:void(0)"' +
                            ' onclick="addToStatementTable(\'' + row.itemId + '\')"><i class="fa fa-plus"></i></a>');
                        actions.push('<a  class="btn btn-danger btn-xs " href="javascript:void(0)"' +
                            ' onclick="decrItem(\'' + row.itemId + '\')"><i class="fa fa-minus"></i></a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    var prefix = ctx + "/serviceItem";
    $(function () {
        var options = {
            uniqueId: "id",
            id: "serviceItem-table",
            url: prefix + "/query?id=1",
            modalName: "养修",
            columns: [
                {
                    field: 'id',
                    title: '服务项id',
                    visible: false
                },
                {
                    field: 'name',
                    title: '服务项名称',
                    sortable: true
                },
                {
                    field: 'originalPrice',
                    title: '服务项价格',
                    sortable: true
                },
                {
                    field: 'info',
                    title: '备注',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 0, "open");
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a  class="btn btn-info btn-xs " href="javascript:void(0)"' +
                            ' onclick="addToStatementTable(\'' + row.id + '\')"><i class="fa fa-plus"></i></a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);

    });

    //加
    function addToStatementTable(id) {
        //拿到点击的id,并且通过api拿到这条数据
        var data = $('#serviceItem-table').bootstrapTable("getRowByUniqueId", id);
        var rowData = {itemId: id, itemName: data.name, itemPrice: data.originalPrice, itemQuantity: 1};

        var itemTable = $('#item-table').bootstrapTable("getRowByUniqueId", id);
        if (itemTable) {
            // 如果已经存在的话把数量加1
            $('#item-table').bootstrapTable('updateCellByUniqueId', {
                id: id,
                field: 'itemQuantity',
                value: itemTable.itemQuantity + 1
            })
        } else {
            //把点击到的数据添加到另外一个表格里面
            $('#item-table').bootstrapTable("append", rowData);
        }
        updateAmount();
    }

    //减
    function decrItem(id) {
        var itemTable = $('#item-table').bootstrapTable("getRowByUniqueId", id);
        if (itemTable.itemId != null) {
            if (itemTable.itemQuantity > 1) {
                // 如果已经存在的话把数量加1
                $('#item-table').bootstrapTable('updateCellByUniqueId', {
                    id: id,
                    field: 'itemQuantity',
                    value: itemTable.itemQuantity - 1
                })
            } else {
                //如果为空就删除掉
                $('#item-table').bootstrapTable("removeByUniqueId", id);
            }
        }
        updateAmount();
    }

    //随着点加或者减的货金额会随着修改
    function updateAmount() {
        //获取到全部的数据
        var data = $('#item-table').bootstrapTable('getData', false);
        var totalAmount = 0;
        $.each(data, function (index, item) {
            totalAmount += item.itemPrice * item.itemQuantity;
        });
        //总金额
        $('#totalAmount').html(totalAmount);
        //优惠金额
        var discountAmount = $('#discountAmount').val();

        var val = $('#totalAmount').val();

        if (val == 0) {
            $('#actuallyPaid').text(0);
        }

        if (!discountAmount) {
            $("#discountAmount").val(0);
            return;
        }


        if (discountAmount < 0) {
            $("#discountAmount").val(0);
            return;
        }
        // 优惠金额不能大于总金额
        if (totalAmount < discountAmount) {
            $("#discountAmount").val(0);
            return;
        }

        // 实际金额
        $('#actuallyPaid').html(totalAmount - discountAmount);
    }
</script>
</html>